'use strict';

angular.module('Common').controller('EntendEnumController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseEnum', 'BaseFunction', '$location', 'Authentication',
    function($scope, $stateParams, BaseModule, BaseEnum, BaseFunction, $location, Authentication) {
        //$scope.authentication = Authentication;
        var setOptions = function(){
            $scope.Module._ExtendEnumValue.Records = [];
            var enumValues = [];
            if($scope.Module.ExtendEnum.FocusedRecord.Options != undefined){
                enumValues = $scope.Module.ExtendEnum.FocusedRecord.Options;
            }
            if(enumValues.length > 0){
                for(var i=0; i<enumValues.length; i++){
                    var newRecord = $scope.Module._ExtendEnumValue.add();
                    newRecord.Value = enumValues[i].Value;
                    newRecord.Name = enumValues[i].Name;
                }
            }
            $scope.Module._ExtendEnumValue.emitOnLoadEvent();
        };

        $scope.btnFindChange = function(){
            if($scope.Module.ExtendEnum.FocusedRecord != null){
                setOptions();
            }
        };

        $scope.btnSave = function(){
            if($scope.Module.ExtendEnum.FocusedRecord.Code == undefined || 
                $scope.Module.ExtendEnum.FocusedRecord.Code.trim() == ""){
                BaseFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.ExtendEnum.FocusedRecord.Name == undefined || 
                $scope.Module.ExtendEnum.FocusedRecord.Name.trim() == ""){
                BaseFunction.alert("名称不能为空！", "保存错误");
                return;
            }

            if($scope.Module._ExtendEnumValue.Records.length == 0){
                BaseFunction.alert("枚举值不能为空！", "保存错误");
                return;
            }
            else{
                var opations = [];
                for(var i=0;i <$scope.Module._ExtendEnumValue.Records.length; i++){
                    if($scope.Module._ExtendEnumValue.Records[i].Value == undefined || 
                        $scope.Module._ExtendEnumValue.Records[i].Value == null || 
                        $scope.Module._ExtendEnumValue.Records[i].Value === ""){
                        BaseFunction.alert("枚举行值不能为空！", "保存错误");
                        return;
                    }
                    if($scope.Module._ExtendEnumValue.Records[i].Name.trim() == ""){
                        BaseFunction.alert("名称不能为空！", "保存错误");
                        return;
                    }

                    for(var j=0; j<opations.length; j++){
                        if(opations[j].Value == $scope.Module._ExtendEnumValue.Records[i].Value){
                            BaseFunction.alert("枚举值不能相同！", "保存错误");
                            return;
                        }
                    }

                    opations.push({
                        Value: $scope.Module._ExtendEnumValue.Records[i].Value,
                        Name: $scope.Module._ExtendEnumValue.Records[i].Name
                    });
                }
            }

            if(opations.length == 0){
                BaseFunction.alert("枚举值不能为空！", "保存错误");
                return;
            }
            $scope.Module.ExtendEnum.FocusedRecord.Options = opations;
            $scope.Module.ExtendEnum.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.ExtendEnum.cancel(function(){
                    setOptions();
                }
            );
        };
        $scope.btnAdd = function(){
            $scope.Module.ExtendEnum.FocusedRecord = $scope.Module.ExtendEnum.add();
            $scope.Module.ExtendEnum.FocusedRecord.Options = [];
            setOptions();
        };
        $scope.btnDelete = function(){
            $scope.Module.ExtendEnum.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/ExtendEnum/List');
        };
        $scope.pageChange = function(){
            setOptions();
        };
        
        var setDefaultValue = function(record){
            var maxLineValue = -1;
            for(var i=0; i<$scope.Module._ExtendEnumValue.Records.length; i++){
                if($scope.Module._ExtendEnumValue.Records[i].Value !== "" && 
                    $scope.Module._ExtendEnumValue.Records[i].Value != undefined && 
                    $scope.Module._ExtendEnumValue.Records[i].Value > maxLineValue)
                {
                    maxLineValue = $scope.Module._ExtendEnumValue.Records[i].Value;
                }
            }
            record.Value = maxLineValue + 1;
            $scope.rowStatusChange();
        };
        $scope.rowStatusChange = function(){
            if($scope.Module.ExtendEnum.FocusedRecord.RowStatus != BaseEnum.RowStatus.Insert.Value){
                $scope.Module.ExtendEnum.FocusedRecord.RowStatus = BaseEnum.RowStatus.Modify.Value;
            }
        };

        //初始化
        BaseModule.initModule($scope, 'ExtendEnum');
        BaseModule.initModule($scope, '_ExtendEnumValue');
        $scope.Module._ExtendEnumValue.PageSize = 10;
        $scope.Module._ExtendEnumValue.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
        $scope.Module._ExtendEnumValue.PageNum = 0;
        $scope.Module._ExtendEnumValue.AfterOnAdd = setDefaultValue;

        if($stateParams.id) {
            $scope.Module.ExtendEnum.findById($stateParams.id, function(record){
                if(record != null){
                    $scope.Module.ExtendEnum.FocusedRecord = record;
                    setOptions();
                }
                else{
                    $scope.btnAdd();
                }
            });
        }
        else{
            $scope.btnAdd();
        };
    }
]);
